//
//  SCPopoverAnimation.swift
//  GZWeibo007
//
//  Created by newuser on 16/1/1.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

///控制model出的动画时间
class SCPopoverAnimation: NSObject, UIViewControllerAnimatedTransitioning{

    //转场动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        //获取model的控制器view
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //将model出来的控制器view添加到容器视图
        transitionContext.containerView()?.addSubview(toView)
        
        //设置view方向的缩放
        toView.transform = CGAffineTransformMakeScale(1, 0)
        //修改锚点
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        //做动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            // 清空transform
            toView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                // 调用completeTransition告诉系统转场动画完成了
                transitionContext.completeTransition(true)
        }
    }
}
